#include "accountloginresponse.h"

#include <QVariant>


namespace Account {

LoginResponse::LoginResponse(QObject *parent):
    JSONResponse(parent)
{
}

Network::Response::Type LoginResponse::type() const
{
    return Response::Login;
}

void LoginResponse::parse(const QVariant &data)
{
    if (data.type() != QVariant::Map){
        setError(Response::Property, "The error code field isn't competitived");
        return;
    }

    QVariantMap map = data.toMap();
    if (!map.contains("uid")) {
        setError(Response::Property, "The \"uid\" value missing");
        return;
    }

    QVariant userId = map.value("uid");
    if (userId.type() != QVariant::Int) {
        setError(Response::Property, "The \"uid\" value isn't competitived");
        return;
    }

    if (!map.contains("urn")) {
        setError(Response::Property, "The \"urn\" value missing");
        return;
    }

    QVariant userName = map.value("urn");
    if (userName.type() != QVariant::String) {
        setError(Response::Property, "The \"urn\" value isn't competitived");
        return;
    }

    if (!map.contains("dpn")) {
        setError(Response::Property, "The \"dpn\" value is missing");
        return;
    }

    QVariant displayName = map.value("dpn");
    if (displayName.type() != QVariant::String) {
        setError(Response::Property, "The \"dpn\" value isn't competitived");
        return;
    }

    if (!map.contains("avatar")) {
        setError(Response::Property, "The \"avatar\" value is missing");
        return;
    }

    QVariant avatar = map.value("avatar");
    if (avatar.type() != QVariant::String) {
        setError(Response::Property, "The \"avatar\" value isn't competitived");
        return;
    }

    if (!map.contains("skey")) {
        setError(Response::Property, "The \"skey\" value missing");
        return;
    }

    QVariant sessionKey = map.value("skey");
    if (sessionKey.type() != QVariant::String) {
        setError(Response::Property, "The \"skey\" value isn't competitived");
        return;
    }

    m_userId = userId.toInt();
    m_userName = userName.toString();
    m_displayName = displayName.toString();
    m_avatar = avatar.toString();
    m_sessionKey = sessionKey.toString();

}

} // namespace Account

